//https://dart.cn/language/#async

/**
使用 async 和 await 关键字可以避免回调地狱 (Callback Hell) 并使代码更具可读性
 */

import 'dart:io';
import 'dart:async';

Future<void> createDescriptions(Iterable<String> objects) async {
  for (final object in objects) {
    try {
      var file = File('$object.txt');
      if (await file.exists()) {
        var modified = await file.lastModified();
        print(
          'File for $object already exists. It was modified on $modified.',
        );
        continue;
      }
      await file.create();
      await file.writeAsString('Start describing $object in this file.');
      print('Created and initialized file for $object.');
    } on FileSystemException catch (e) {
      print('Cannot create description for $object: $e');
    }
  }
}

void main() async {
  // 创建一个包含对象名称的列表
  var objects = ['Voyager I', 'Voyager II', 'Apollo 11', 'Hubble'];

  // 调用createDescriptions函数
  await createDescriptions(objects);

  // 打印完成信息
  print('Description files creation process completed.');
}